package com.mzp.UserCart.UserCart.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;
import com.mzp.UserCart.UserCart.dao.UserCartDao;
import com.mzp.UserCart.UserCart.pojo.UserCart;

/**
 * @Description user_cart 服务层
 * @author admin
 * @date 2021-04-20 10:01:19
*/
@Service
public class UserCartService {

	@Autowired
	private UserCartDao userCartDao;

	@Autowired
	private IdWorker idWorker;

	/**
	* 查询全部列表
	* @return
	*/
	public List<UserCart> findAll() {
		return userCartDao.findAll();
	}

	/**
	* 条件查询+分页
	* @param whereMap
	* @param page
	* @param size
	* @return
	*/
	public Page<UserCart> findSearch(Map whereMap, int page, int size) {
		Specification<UserCart> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return userCartDao.findAll(specification, pageRequest);
	}

	/**
 	* 条件查询
	* @param whereMap
	* @return
	*/
	public List<UserCart> findSearch(Map whereMap) {
		Specification<UserCart> specification = createSpecification(whereMap);
		return userCartDao.findAll(specification);
	}

	/**
	* 根据ID查询实体
	* @param id
	* @return
	*/
	public UserCart findById(String id) {
		return userCartDao.findById(id).get();
	}

	/**
	* 增加
	* @param userCart
	*/
	public void add(UserCart userCart) {
		userCart.setId( idWorker.nextId()+"" );
		userCartDao.save(userCart);
	}

	/**
	* 修改
	* @param userCart
	*/
	public void update(UserCart userCart) {
		userCartDao.save(userCart);
	}

	/**
	* 删除
	* @param id
	*/
	public void deleteById(String id) {
		userCartDao.deleteById(id);
	}

	/**
	* 动态条件构建
	* @param searchMap
	* @return
	*/
	private Specification<UserCart> createSpecification(Map searchMap) {

		return new Specification<UserCart>() {

			@Override
			public Predicate toPredicate(Root<UserCart> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
				//userid
				if (searchMap.get("userid")!=null && !"".equals(searchMap.get("userid"))) {
					predicateList.add(cb.like(root.get("userid").as(String.class), "%"+(String)searchMap.get("userid")+"%"));
				}
				//goodsid
				if (searchMap.get("goodsid")!=null && !"".equals(searchMap.get("goodsid"))) {
					predicateList.add(cb.like(root.get("goodsid").as(String.class), "%"+(String)searchMap.get("goodsid")+"%"));
				}
				//goodstotal
				if (searchMap.get("goodstotal")!=null && !"".equals(searchMap.get("goodstotal"))) {
					predicateList.add(cb.like(root.get("goodstotal").as(String.class), "%"+(String)searchMap.get("goodstotal")+"%"));
				}
				//pay_status
				if (searchMap.get("payStatus")!=null && !"".equals(searchMap.get("payStatus"))) {
					predicateList.add(cb.like(root.get("payStatus").as(String.class), "%"+(String)searchMap.get("payStatus")+"%"));
				}
				//id_del
				if (searchMap.get("idDel")!=null && !"".equals(searchMap.get("idDel"))) {
					predicateList.add(cb.like(root.get("idDel").as(String.class), "%"+(String)searchMap.get("idDel")+"%"));
				}
				//isin
				if (searchMap.get("isin")!=null && !"".equals(searchMap.get("isin"))) {
					predicateList.add(cb.like(root.get("isin").as(String.class), "%"+(String)searchMap.get("isin")+"%"));
				}

				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
			}
		};
	}

}
